package com.linktrip.devicemanager.control.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.google.gson.Gson;
import com.linktrip.devicemanager.entity.UserInfo;
import com.linktrip.devicemanager.server.UserService;
import com.linktrip.devicemanager.util.Constant;
import com.linktrip.devicemanager.util.ToolKits;

@Controller
@RequestMapping(value = "/service/user")
public class UserControl {
	Logger log = LoggerFactory.getLogger(UserControl.class);
	@Autowired
	private UserService server;

	@RequestMapping(value = "/checkLogin.lt")
	@ResponseBody
	public String checkLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		boolean result = false;
		String name = ToolKits.getRequestParamValue(request, "name");
		String pwd = ToolKits.getRequestParamValue(request, "pwd");
		UserInfo user = server.getUserByName(name);
		if (user != null) {
			if (user.getUser_passwd() != null) {
				if (user.getUser_passwd().equals(pwd)) {
					result = true;
					session.setAttribute(Constant.USER_SESSION, user);
				}
			}
		}

		int res = result ? Constant.RESPONSE_OK : Constant.RESPONSE_ERR;
		Gson gson = new Gson();
		return gson.toJson(res);
	}

	@RequestMapping(value = "/loginOut.lt")
	public String loginOut(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
//		boolean result = true;
//		String basePath = "";
		try {
			request.getSession().removeAttribute(Constant.USER_SESSION);
			request.getSession().invalidate();
//			String path = request.getContextPath();
			// 拼接跳转页面路径
//			basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path
//					+ "/";
//			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/service/redirect/loginPage.lt";
	}

	@RequestMapping(value = "/updateUserPWD.lt")
	@ResponseBody
	public String updateUserInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		int res = Constant.RESPONSE_ERR;
		UserInfo user = (UserInfo) session.getAttribute(Constant.USER_SESSION);
		String pwd = ToolKits.getRequestParamValue(request, "pwd").trim();
		String pwd_new = ToolKits.getRequestParamValue(request, "pwd_new").trim();
		if (!ToolKits.isEmpty(pwd)) {
			if (pwd.equals(user.getUser_passwd())) {
				user.setUser_passwd(pwd_new);
				try {
					server.updateUserPW(user);
					res = Constant.RESPONSE_OK;
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				res = Constant.RESULT_NOT_MATCH;
			}
		}

		Gson gson = new Gson();
		return gson.toJson(res);
	}

	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}

}
